/**
 * 给定时器增加一个hook函数 用来全局清除
 */

import { useState, useEffect } from 'react';
function useClearTimeOut() {
  const [timerList, setTimerList] = useState([]);

  useEffect(() => {
    return () => {
      timerList.forEach(r => clearTimeout(r));
    };
  }, [timerList]);

  const addTimerCallBack = func => {
    const newTimer = setTimeout(() => {
      func();
    });
    let cloneTimerList = [...timerList];
    cloneTimerList.push(newTimer);
    setTimerList(cloneTimerList);
  };

  return addTimerCallBack;
}
export default useClearTimeOut;
